﻿using EF2Demo.Models;
using EF2Demo.WxModels;
using Newtonsoft.Json;
using System;
using System.Dynamic;
using System.Linq;

namespace EF2Demo
{
    internal class Program
    {
        static void Main(string[] args)
        {
            MyContext myContext = new MyContext();

            foreach (WxMessage item in myContext.WxMessages)
            {
                Console.WriteLine(item.touser);
                SendMessageModel smm = GetSendMessageModel(item);

                //dynamic test = new SendMessageModel();
                //test.text = "13";

                string str = JsonConvert.SerializeObject(smm);
                string str2 = JsonConvert.SerializeObject(GetSSM(item));
                Console.WriteLine(str);
                Console.WriteLine(str2);
                Console.WriteLine(item.message);

                Console.WriteLine("--------------------------------");
                string result = str.Insert(str.Length - 1, ",\"text\":" + item.message);
                Console.WriteLine(result);
                break;
            }
            Console.WriteLine("--------------------------------");

            var userList = myContext.Database.SqlQuery<WxUser>("SELECT TOP 10 * FROM WxUser").ToList();
            foreach (var item in userList)
            {
                Console.WriteLine(item.userid + "-" + item.name + "-" + item.position);
            }

            Console.ReadLine();
        }

        public static SendMessageModel GetSendMessageModel(WxMessage wxMessage)
        {
            SendMessageModel sendMessageModel = new SendMessageModel()
            {
                touser = wxMessage.touser,
                toparty = wxMessage.toparty,
                totag = wxMessage.totag,
                msgtype = wxMessage.msgtype,
                agentid = wxMessage.agentid,
                safe = wxMessage.safe,
                enable_id_trans = wxMessage.enable_id_trans,
                duplicate_check_interval = wxMessage.duplicate_check_interval,
            };

            return sendMessageModel;
        }

        public dynamic GetDynamic()
        {
            dynamic dynEO = new ExpandoObject();//初始化一个不包含任何成员的ExpandoObject
            dynEO.number = 20;
            dynEO.MeThod = new Func<int, string>(i => { return (i + 20).ToString(); });
            return dynEO;
        }

        public static dynamic GetSSM(WxMessage wxMessage)
        {
            dynamic ssm = new ExpandoObject();
            ssm.text = wxMessage.message;
            ssm.touser = wxMessage.touser;
            ssm.toparty = wxMessage.toparty;
            ssm.totag = wxMessage.totag;
            ssm.msgtype = wxMessage.msgtype;
            ssm.agentid = wxMessage.agentid;
            ssm.safe = wxMessage.safe;
            ssm.enable_id_trans = wxMessage.enable_id_trans;
            ssm.duplicate_check_interval = wxMessage.duplicate_check_interval;
            //((IDictionary<string, object>)ssm).Add("Name", "张三");
            return ssm;
        }
    }
}
